/*
 * @Description:
 * @Autor: fage
 * @Date: 2022-06-06 13:52:37
 * @LastEditors: fage
 * @LastEditTime: 2022-07-07 10:45:55
 */

const dirList = [];
var vm = new Vue({
  el: "#app",
  data: {
    loading: false,
    list: [],
    newItem: {},
    rules: {
      password: [
        { required: true, message: "请输入回调词语", trigger: "blur" },
      ],
      deployKeyword: [
        { required: true, message: "请输入触发词语", trigger: "blur" },
      ],
      path: [{ required: true, message: "请输入网站目录", trigger: "blur" }],
      exec: [{ required: true, message: "请输入执行命令", trigger: "blur" }],
    },
    props: {
      label: "name",
      children: "zones",
      isLeaf: "leaf",
    },
  },
  beforeMount: function () {
    this.initNewItem();
    this.loadList();
  },
  mounted() {},
  methods: {
    initNewItem() {
      this.newItem = {
        password: "",        
        waitExecComplete: true,
        path: "",
        deployKeyword: "",
        exec: "",
        deployKeyword2: "",
        exec2: "",
        loading: false,
      };
    },
    loadList() {
      const that = this;
      let params = {
        way: "website",
        action: "list",
      };
      ajax.post("/api/manage", params).then((t) => {
        console.log("data:", t);
        if (t.msg == "ok") {
          that.list = that.formatList(t.data);
        } else {
          that.$alert("操作失败:" + t.msg);
        }
      }, console.log);
    },
    del(id) {
      const that = this;
      that.$confirm("确定要删除吗？", {
        callback: function (a) {
          if (a == "cancel") {
            return;
          }
          const params = {
            way: "website",
            action: "del",
            id: id,
          };
          ajax.post("/api/manage", params).then((t) => {
            console.log("data:", t);
            if (t.msg == "ok") {
              that.list = that.formatList(t.data);
            } else {
              that.$alert("操作失败:" + t.msg);
            }
          }, console.log);
        },
      });
    },
    submitForm(formName) {
      const that = this;
      console.log(formName);
      let params;
      if (formName == "newItem") {
        that.newItem.loading = true;
        params = JSON.stringify(that.newItem);
        params = JSON.parse(params);
      } else {
        let item = this.list.find((t) => t.id == formName);
        params = item;
      }
      for (k in that.rules) {
        console.log(k);
        if (!params[k]) {
          return that.$alert(that.rules[k][0].message, { type: "error" });
        }
      }
      params.way = "website";
      params.action = "save";
      ajax.post("/api/manage", params).then((t) => {
        console.log("data:", t);
        if (t.msg == "ok") {
          that.initNewItem();
          that.list = that.formatList(t.data);
          that.$message({
            message: "保存成功",
            type: "success",
          });
        } else {
          that.$alert("操作失败:" + t.msg);
        }
      }, console.log);
    },
    formatList(list) {
      let arr = [];
      if (!Array.isArray(list)) {
        for (key in list) {
          arr.push(list[key]);
        }
      } else {
        arr = list;
      }
      arr.forEach((t) => {
        t.loading = false;
        t.exec = t.exec.join("\n");
        t.exec2 = t.exec2.join("\n");
      });
      console.log("arr", arr);
      return arr;
    },
    loadNode(node, resolve) {
      // if (node.level === 0) {
      //   return resolve([{ name: 'region' }]);
      // }
      // if (node.level > 1) return resolve([]);

      // setTimeout(() => {
      //   const data = [{
      //     name: 'leaf',
      //     leaf: true
      //   }, {
      //     name: 'zone'
      //   }];

      //   resolve(data);
      // }, 500);

      console.log(node);
      const that = this;
      let dir = dirList[node.id];
      console.log("dirList", dirList);
      if (!dir) {
        dir = "/";
      }
      let params = {
        way: "website",
        action: "getDir",
        dir,
      };
      ajax.post("/api/manage", params).then((t) => {
        console.log("data:", t);
        if (t.msg == "ok") {
          let arr = [];
          for (let i = 0; i < t.data.length; i++) {
            let dir = t.data[i];
            dirList.push(dir);
            dir = dir.split("/");
            dir = dir[dir.length - 1];
            arr.push({ name: dir });
          }
          resolve(arr);
        } else {
          that.$alert("操作失败:" + t.msg);
        }
      }, console.log);
    },
  },
});
